package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Expend;
import cn.wolfcode.crm.service.IExpendService;
import cn.wolfcode.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("expend")
public class ExpendController {

    @Autowired
    IExpendService expendService;

    @RequestMapping("")
    public String index(){
        return "expend/listDir";
    }

    @RequestMapping("list")
    public String query(Model model, Long parent_id) {
        List<Expend> expends = expendService.selectExpend();
        for (Expend expend : expends) {
            parent_id = expend.getId();
        }
        List<Expend> expendPoints = expendService.selectExpendPoint(parent_id);
        model.addAttribute("expends", expends);
        model.addAttribute("expendPoints", expendPoints);
        return "expend/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Expend expend) {
        JsonResult result = new JsonResult();
        try {
            if (expend.getId() == null) {
                expendService.insert(expend);
            } else {
                expendService.updateByPrimaryKey(expend);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("保存失败!");
        }
    }

    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            if (id != null) {
                expendService.deleteByPrimaryKey(id);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("删除失败");
        }
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Expend> selectAll() {

        return  expendService.selectAll();

    }
    @RequestMapping("selectExpend")
    @ResponseBody
    public List<Expend> selectExpend() {

        return  expendService.selectExpend();

    }
}

